Ext.ns("BookManagerView");
BookManagerView = Ext.extend(Ext.Panel,{
		westPanel:null,
		innerSearchPanel:null,
		innerGridPanel:null,
		innerPanel:null,
			constructor : function(a) {
				Ext.applyIf(this, a);
				this.initUIComponents();
				BookManagerView.superclass.constructor.call(this, {
					id : "BookManagerView",
				title : "书籍管理",
				iconCls : "menu-role",
				region : "center",
				layout : "border",
				items : [this.innerPanel]
			});
		},
		initUIComponents : function() {
			var root = new Ext.tree.AsyncTreeNode({
				text : "书籍分类",
				id : 'e4e4e40c385cb07401385ccdc4db0002',
				expanded : true
			});
			var loader = new Ext.tree.TreeLoader({
				dataUrl : __ctxPath + '/searchCatalog.do?bookid=e4e4e40c385cb07401385ccdc4db0002',
				requestMethod : 'POST'
			});
			this.westPanel = new Ext.tree.TreePanel({
				root : root,
				title : '<span>书籍分类</span>',
				region : 'west',
				id : 'catalogTree',
				split : true,
				width : 220,
				minSize : 180,
				maxSize : 300,
				collapsible : true,
				autoScroll : true,
				animate : false,
				loader : loader,
				listeners : {
					"click" : function(node) {
					},
					"beforeload" : function(node) {
						Ext.getCmp("catalogTree").loader.dataUrl =  __ctxPath+'/searchCatalog.do?bookid='+node.id
					}
				}
			});
			this.innerSearchPanel = new Ext.Panel({  
					region : 'center',
					items:[new Ext.FormPanel({  
							height : 35,
							region : "center",
							frame : false,
							layoutConfig : {
								padding : "5",
								align : "middle"
							},
							id : "BookSearchForm",
							layout : "hbox",
							defaults : {
								xtype : "label",
								border : false,
								margins : {top : 2,right : 4,bottom : 2,left : 4}
							},
							items : [{text : "书籍名称"},
									{xtype : "textfield",name : "Q_name_S_LK"},
									{xtype : "button",text : "查询",iconCls : "search",
										handler : function() {
											var c = Ext.getCmp("BookSearchForm");
											var d = Ext.getCmp("BookGrid");
											if (c.getForm().isValid()) {
												c.getForm().submit({
													waitMsg : "正在提交查询",
													url : __ctxPath+ "/system/listBook.do",
													success : function(f,g) {
														var e = Ext.util.JSON.decode(g.response.responseText);
														d.getStore().loadData(e);
													}
												});
											}
										}
									} ]
                        })]
                });  
			this.store = new Ext.data.Store( {
				proxy : new Ext.data.HttpProxy( {
					url : __ctxPath + "/listBook.do"
				}),
				reader : new Ext.data.JsonReader( {
					root : "result",
					totalProperty : "totalCounts",
					id : "id",
					fields : [ {name : "id",type : "string"}, "name", "desc", 
					           {name : "status",type : "stirng"}, "isDefaultIn" ]
				}),
				remoteSort : true
			});
			this.store.setDefaultSort("id", "desc");
			this.store.load( {params : {start : 0,limit : 25}});
			var b = new Ext.grid.CheckboxSelectionModel();
			var a = new Ext.grid.ColumnModel({
					columns : [b,new Ext.grid.RowNumberer(),
					{header : "id",dataIndex : "id",id:"id",name:"id",hidden : true},									
					{header : "书籍名称",id:"name",name:"name",dataIndex : "name",width : 200},
					{header : "作者",dataIndex : "author",width : 400,id:"author",name:"author"},
					{header : "出版社",dataIndex : "publishing",width : 400,id:"publishing",name:"publishing"},
					{header : "出版时间",dataIndex : "publishingDateStr",width : 400,id:"publishingDateStr",name:"publishingDateStr"},
					{header : "管理",dataIndex : "id",width : 80,
						renderer : function(j, i, e, g,k) {
							var c = "'"+e.data.id+"'";
							var name="'"+e.data.name+"'";
							var f = '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="BookManagerView.edit(' + c + ')"></button>';
							f+='&nbsp;<button title="章节管理" value=" " class="btn-edit"  onclick="BookManagerView.catalogManager(' + c + ','+name+')"></button>';
							return f;
						}
					} ],
					defaults : {sortable : true,menuDisabled : false,width : 100}
			});
            this.innerGridPanel = new Ext.Panel({  
                        region : 'south',  
                       	height : document.body.clientHeight - 160,
                        items:[
                               new Ext.grid.GridPanel({
       							id : "BookGrid",
       							bodyStyle : 'width:100%',  
                        		autoWidth : true,  
       							autoHeight : true,
                        		autoScroll : true,  
       							tbar : this.topbar(),
       							store : this.store,
       							trackMouseOver : true,
       							disableSelection : false,
       							loadMask : true,
       							cm : a,
       							sm : b,
       							viewConfig : {forceFit : true,enableRowBody : false,showPreview : false},
       							bbar : new Ext.PagingToolbar( {
       								pageSize : 25,
       								store : this.store,
       								displayInfo : true,
       								displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
       								emptyMsg : "当前没有记录"
       							})
       						})
                               ]
                    });  
            this.innerPanel = new Ext.Panel({  
                region : 'center',  
                margins : '5 0 0 0',  
                layout : 'border',  
                items : [this.innerSearchPanel, this.innerGridPanel]  
            });  
		}
});
BookManagerView.prototype.topbar = function() {
	var a = new Ext.Toolbar( {
		id : "BookFootBar",
		height : 30,
		bodyStyle : "text-align:left",
		items : []
	});
	if (isGranted("_BookAdd")) {
		a.add(new Ext.Button( {
			iconCls : "btn-add",
			text : "添加书籍",
			handler : function() {
				new BookForm().show();
			}
		}));
	}
	if (isGranted("_BookDel")) {
		a.add(new Ext.Button(
				{
					iconCls : "btn-del",
					text : "删除书籍",
					handler : function() {
						var d = Ext.getCmp("BookGrid");
						var b = d.getSelectionModel().getSelections();
						if (b.length == 0) {
							Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
							return;
						}
						var e = Array();
						var f = "";
						for ( var c = 0; c < b.length; c++) {
							if (b[c].data.id != "") {
								e.push(b[c].data.id);
							} else {
								f += b[c].data.name + ",";
							}
						}
						if (e.length>0) {
							BookManagerView.remove(e);
						} else {
							Ext.ux.Toast.msg("信息", f + "不能被删除！");
						}
					}
				}));
	}
	return a;
};
BookManagerView.remove = function(b) {
	var a = Ext.getCmp("BookGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
			if (c == "no") {
				return
			}
			Ext.Ajax.request( {
				url : __ctxPath + "/deleteBook.do",
				params : {
					ids : b
				},
				method : "post",
				success : function() {
					Ext.ux.Toast.msg("信息", "成功删除所选记录！");
					a.getStore().reload( {
						params : {
							start : 0,
							limit : 25
						}
					});
				}
			});
	});
};
BookManagerView.edit = function(a) {
	new BookForm( {
		id:a
	}).show();
};
BookManagerView.catalogManager = function(a,n) {
	new CatalogManager({
		bookid:a,
		bookName:n
	}).show();
};